package com.heima.wemedia.service.impl;

import apis.article.IArticleClient;
import com.alibaba.fastjson.JSON;
import com.heima.common.constants.WemediaConstants;
import com.heima.model.article.vos.ApArticleVo;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.CommentPageDto;
import com.heima.model.wemedia.dtos.CommentStatusManageDto;
import com.heima.model.wemedia.vos.CommentWemediaNewVo;
import com.heima.utils.thread.ThreadLocalUtil;
import com.heima.wemedia.pojo.ApComment;
import com.heima.wemedia.service.CommentManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CommentManageServiceImpl
 * @Description
 * @Author yangjiangping
 * @Date 2023/6/28 14:05
 * @Version 1.0
 **/
@Service
@Transactional
@Slf4j
public class CommentManageServiceImpl implements CommentManageService{

    @Autowired
    private IArticleClient iArticleClient;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 自媒体端文章评论管理--分页查询所有文章评论信息接口
     *
     * @param dto
     * @return com.heima.model.common.dtos.ResponseResult
     * @Description
     * @Author YangJiangPing
     * @Date 14:07 2023/6/28
     * @Param [dto]
     */
    @Override
    public ResponseResult findNewsComments(CommentPageDto dto) {
        //1:参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getSize() == null || dto.getSize() <= 0) {
            dto.setSize(10);
        } else {
            dto.setSize(Math.min(dto.getSize(), 50));
        }
        dto.setArticleId(Long.valueOf(String.valueOf(ThreadLocalUtil.get())));
        //2:查询所有信息
        //2.2查询所有文章信息
//        System.out.println(demo);
        PageResponseResult newsComments = iArticleClient.findNewsComments(dto);
        //2.3: 返回数据校验
        if (200 != newsComments.getCode() || newsComments.getData() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //3:处理返回数据
        String jsonString = JSON.toJSONString(newsComments.getData());
        List<ApArticleVo> apArticleVos = JSON.parseArray(jsonString, ApArticleVo.class);
        List<CommentWemediaNewVo> collect = apArticleVos.stream().map(article -> {
            CommentWemediaNewVo commentWemediaNewVo = new CommentWemediaNewVo();
            commentWemediaNewVo.setId(article.getId().toString());
            commentWemediaNewVo.setTitle(article.getTitle());
            Date createdTime = article.getCreatedTime();
//            long time = createdTime.getTime();
//            String s = String.valueOf(time);
//            int i = Integer.parseInt(s);
//            commentWemediaNewVo.setCreatedTime((int)Math.min(i,16232240265L));
            commentWemediaNewVo.setComment(article.getIsComment());
            commentWemediaNewVo.setComments(article.getComment());
            return commentWemediaNewVo;
        }).collect(Collectors.toList());

/*        StringJoiner id = new StringJoiner(",");
        StringJoiner title = new StringJoiner(",");
        apArticleList.stream().forEach(article -> {
            id.add(article.getId().toString());
            title.add(article.getTitle());

        commentWemediaNewVo.setId(id.toString());
        commentWemediaNewVo.setTitle(title.toString());
        commentWemediaNewVo.setComment(true);*/

        //4: 处理返回数据
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),newsComments.getTotal());
        result.setData(collect);
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 打开或关闭评论接口
     *
     * @param dto
     * @return com.heima.model.common.dtos.ResponseResult
     * @Description
     * @Author YangJiangPing
     * @Date 9:03 2023/6/29
     * @Param [dto]
     */
    @Override
    public ResponseResult updateCommentStatus(CommentStatusManageDto dto) {
        //1: 校验参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2: 修改文章配置表状态为不可评论,修改文章表评论数量为0
        String json = JSON.toJSONString(dto);
        kafkaTemplate.send(WemediaConstants.COMMENT_UPDATE_IS_COMMENT_TOPIC,json);
        //3: 删除mongodb数据
        mongoTemplate.remove(Query.query(Criteria.where("entryId").is(dto.getArticleId())), ApComment.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

